//
//  EditBillTypeViewController.m
//  iEasyBill
//
//  Created by student on 14-5-6.
//  Copyright (c) 2014年 com.yly16. All rights reserved.
//

#import "EditBillTypeViewController.h"
#define KStarListFile @"iBillType.plist"
@interface EditBillTypeViewController ()
{

}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong , nonatomic)iBillType *aType;
@property (strong , nonatomic)iBillAccount *iAccount;
@end

@implementation EditBillTypeViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    //self.billTypeList = [[DatabaseManager shareManager]showAllBillType];
    
//    self.billTypeList = [[iBillTypeManager shareManager]TypeList];
    id type = [self.billTypeList firstObject];
    if ([type isKindOfClass:[iBillType class]]) {
        self.title = @"账单父类别";
    }
    else{
        self.title = @"账单账户";
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    if (self.aType.typeName.length != 0) {
        [self.billTypeList insertObject:self.aType atIndex:0];
        [self.tableView reloadData];
        self.aType = nil;
    }
    
}

- (IBAction)addOneCell:(id)sender {

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    
    return self.billTypeList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   

    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    iBillType *type = self.billTypeList[indexPath.row];
    
    UIImageView *imageView = (UIImageView*)[cell viewWithTag:11];
    UILabel *Namelable = (UILabel *)[cell viewWithTag:12];
    
    if ([type isKindOfClass:[iBillType class]]) {
        
        imageView.image = [UIImage imageWithData:type.typeImageData];
        
        Namelable.text = type.typeName;
    }
    else{
        iBillAccount *account = self.billTypeList[indexPath.row];
        cell.textLabel.text = account.accountName;
        imageView.image = [UIImage imageWithData:account.accountImageData];
        
        Namelable.text = account.accountName;
    }
    return cell;
   
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [[DatabaseManager shareManager]removeABillType:[self.billTypeList objectAtIndex:indexPath.row]];
        [self.billTypeList removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    UIViewController *NewVC = [segue destinationViewController];
    NSIndexPath *indexPath = [self.iTableView indexPathForCell:sender];
    NSMutableArray *list = [[NSMutableArray alloc]init];
    
    iBillType *type = self.billTypeList[indexPath.row];
    if ([type isKindOfClass:[iBillType class]]) {
        list = type.typeList;
    }
    else{
        iBillAccount *account = self.billTypeList[indexPath.row];
        list = account.accountList;iBillType *type = self.billTypeList[indexPath.row];
        if ([type isKindOfClass:[iBillType class]]) {
            list = type.typeList;
        }
        else{
            iBillAccount *account = self.billTypeList[indexPath.row];
            list = account.accountList;
        }
    }
    
    if ([segue.identifier isEqualToString:@"2sub"]) {
        [NewVC setValue:list forKey:@"iList"];
    }
    if ([segue.identifier isEqualToString:@"addtype"]) {
        self.aType = [[iBillType alloc]init];
        [NewVC setValue:self.aType forKey:@"aType"];
    }
    if ([segue.identifier isEqualToString:@"addsubtype"]) {
        self.iAccount = [[iBillAccount alloc]init];
        [NewVC setValue:self.iAccount forKey:@"iAccount"];
    }
    
}

 

@end
